package cn.sytton.taffe.web.sys.entity;

import cn.sytton.taffe.framework.base.entity.BaseUpdatedEntity;
import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.util.StringUtils;
import cn.sytton.taffe.framework.validation.Field;
import cn.sytton.taffe.framework.validation.NotDuplicate;

import javax.validation.constraints.*;
import java.sql.Date;

/**
 * 用户entity sys_user
 *
 * @author skyrock
 */
@Schema(description = "用户")
@Data
@EqualsAndHashCode(callSuper = true)
@NotDuplicate(value = {
        @Field(name = {"cellphone"}, message = "手机号码[ %s ]已存在"),
        @Field(name = {"email"}, message = "邮箱地址[ %s ]已存在"),
        @Field(name = {"loginName"}, message = "账号[ %s ]已存在"),
        @Field(name = {"userCode"}, message = "编号[ %s ]已存在")},
        excludeByField="userId")
@TableName("sys_user")
public class UserEntity extends BaseUpdatedEntity {

    @Schema(description = "用户ID(主键)")
    @TableId(type = IdType.ASSIGN_ID)
    private String userId;    // 用户ID 

    @Schema(description = "姓名")
    @NotNull(message = "姓名不能为空")
    private String userName;    // 姓名 

    @Schema(description = "编号")
    @NotNull(message = "编号不能为空")
    private String userCode;    // 编号 

    @Schema(description = "账号")
    @NotNull(message = "账号不能为空")
    private String loginName;    // 账号 

    @Schema(description = "密码", accessMode = Schema.AccessMode.WRITE_ONLY)
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    @NotNull(message = "密码不能为空")
    @TableField(updateStrategy = FieldStrategy.NEVER)
    private String password;    // 密码

    @Schema(description = "状态: 禁用0/启用1")
    private Integer userStatus = 1;    // 状态, 0:禁用, 1:启用

    @Schema(description = "职位ID")
    private String positionId;   // 职位ID

    @Schema(description = "手机号码")
    private String cellphone;    // 手机号码 

    @Schema(description = "邮箱地址")
    @Email
    private String email;    // 邮箱地址 

    @Schema(description = "照片")
    private String photo;    // 照片 

    @Schema(description = "有效期")
    @JsonFormat(pattern = "yyyy-MM-dd")
    private Date expiryDate;    // 有效期

    @Schema(description = "性别: 男1/女0")
    private String gender;    // 性别, 男: 1 女: 0

    public String getCellphone() {
        return StringUtils.hasText(cellphone) ? cellphone : null;
    }

    public String getEmail() {
        return StringUtils.hasText(email) ? email : null;
    }

    //-----以下是关联属性-----

    @Schema(description = "职位名称", accessMode = Schema.AccessMode.READ_ONLY)
    @TableField(exist = false)
    public String positionName;
}
